package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.JobInfo;
import com.bw.entity.JobQuery;
import com.bw.entity.User;
import com.bw.mapper.JobMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ListController {

    @Autowired
    JobMapper jobMapper;

    @RequestMapping("/list.do")
    public String list(JobQuery query, Integer pageNum, HttpServletRequest request, Model model) {
        // 在列表页展示一下  用户的昵称和头像
        // 获取当前登录用户   （request 中获取cookie， 从cookie过滤出token，  解密token得到用户 ）
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        String nickName = userFromRequest.getNickName();
        String avatarUrl = userFromRequest.getAvatarUrl();
        model.addAttribute("nickName", nickName);
        model.addAttribute("avatarUrl", avatarUrl);



        // 1.渲染下拉框
        List<Dict> feedbackList = jobMapper.listFeedback();
        model.addAttribute("feedbackList", feedbackList);
        // 2.分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 10);
        List<JobInfo> list = jobMapper.selectList(query);
        PageInfo<JobInfo> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);
        //3. 条件回显
        model.addAttribute("query",query);
        return "list";
    }


}
